package org.derusse.cycle.activity;

import org.derusse.cycle.CycleMeterApplication;
import org.derusse.cycle.R;
import org.derusse.cycle.event.LocationLiveListener;
import org.derusse.cycle.event.LocationStatsListener;
import org.derusse.cycle.event.LocationTrackListener;
import org.derusse.cycle.model.Live;
import org.derusse.cycle.model.Stats;
import org.derusse.cycle.model.Theme;
import org.derusse.cycle.model.Track;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class CycleMeterActivity extends Activity implements OnClickListener, LocationTrackListener, LocationStatsListener, LocationLiveListener
{

	private CycleMeterApplication application;
	private Track track;
	private Live live;
	private Stats stats;

	private Theme theme;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		this.theme = new Theme(Theme.THEME_FULL);

		setContentView(this.theme.layout);

		if (this.theme.buttonStart != -1)
			((View) findViewById(this.theme.buttonStart)).setOnClickListener(this);
		if (this.theme.buttonPause != -1)
			((View) findViewById(this.theme.buttonPause)).setOnClickListener(this);
		if (this.theme.buttonResume != -1)
			((View) findViewById(this.theme.buttonResume)).setOnClickListener(this);
		if (this.theme.buttonStop != -1)
			((View) findViewById(this.theme.buttonStop)).setOnClickListener(this);

		this.application = (CycleMeterApplication) getApplication();

		this.track = this.application.getTrack();
		this.track.addListener(this);

		this.live = new Live(this.application);
		this.live.addListener(this);

		this.stats = new Stats(this.application);
		this.stats.addListener(this);

		this.UpdateLocation();
		this.UpdateTrack();
		this.UpdateTrackStatus(this.track.getStatus());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		super.onOptionsItemSelected(item);
		switch (item.getItemId())
		{
		case R.id.menu_exit:
			this.finish();
			return true;
		case R.id.menu_history:
			this.ShowHistory();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		this.live.Stop();
		this.stats.Stop();
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		this.live.Stop();
		this.stats.Stop();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		this.live.Start();
		this.stats.Start();
	}

	public void onClick(View view)
	{
		int id = view.getId();
		switch (view.getId())
		{
		default:
			if (id == this.theme.buttonStart)
				this.application.StartTrack();
			else if (id == this.theme.buttonPause)
				this.application.PauseTrack();
			else if (id == this.theme.buttonResume)
				this.application.ResumeTrack();
			else if (id == this.theme.buttonStop)
				this.application.StopTrack();
			break;
		}
	}

	public void onTrackChanged()
	{
		this.UpdateTrack();
	}

	public void onLiveChanged()
	{
		this.UpdateLocation();
	}

	public void onStatsChanged()
	{
		this.UpdateStats();
	}

	public void onLiveStatusChanged(int oldStatus, int newStatus)
	{
		switch (newStatus)
		{
		case Live.STATUS_DISABLED:
			Toast.makeText(getBaseContext(), getString(R.string.toast_gps_disabled), Toast.LENGTH_SHORT).show();
			break;
		case Live.STATUS_READY:
			Toast.makeText(getBaseContext(), getString(R.string.toast_gps_enabled), Toast.LENGTH_SHORT).show();
			break;
		}
	}

	public void onTrackStatusChanged(int oldStatus, int newStatus)
	{
		this.UpdateTrackStatus(newStatus);
	}

	public void UpdateLocation()
	{
		if (theme.textLatitude != -1)
			((TextView) findViewById(theme.textLatitude)).setText(this.live.getLatitudeFormat());
		if (theme.textLongitude != -1)
			((TextView) findViewById(theme.textLongitude)).setText(this.live.getLongitudeFormat());
		if (theme.textSpeed != -1)
			((TextView) findViewById(theme.textSpeed)).setText(this.live.getSpeedFormat());
		if (theme.textAltitude != -1)
			((TextView) findViewById(theme.textAltitude)).setText(this.live.getAltitudeFormat());
		if (theme.textAccuracy != -1)
			((TextView) findViewById(theme.textAccuracy)).setText(this.live.getAccuracyFormat());
	}

	public void UpdateStats()
	{
		if (theme.textDistanceTilt != -1)
			((TextView) findViewById(theme.textDistanceTilt)).setText(this.stats.getTiltFormat());
		if (theme.textSpeedAvg1 != -1)
			((TextView) findViewById(theme.textSpeedAvg1)).setText(this.stats.getAvg1Format());
		if (theme.textSpeedAvg10 != -1)
			((TextView) findViewById(theme.textSpeedAvg10)).setText(this.stats.getAvg10Format());
	}

	public void UpdateTrack()
	{
		if (theme.textDistance != -1)
			((TextView) findViewById(theme.textDistance)).setText(this.track.getDistanceFormat());
		if (theme.textDistanceClimb != -1)
			((TextView) findViewById(theme.textDistanceClimb)).setText(this.track.getClimbFormat());
		if (theme.textSpeedMax != -1)
			((TextView) findViewById(theme.textSpeedMax)).setText(this.track.getSpeedMaxFormat());
		if (theme.textSpeedAvg != -1)
			((TextView) findViewById(theme.textSpeedAvg)).setText(this.track.getSpeedAvgFormat());
	}

	public void UpdateTrackStatus(int newStatus)
	{
		switch (newStatus)
		{
		case Track.STATUS_STARTED:
			mHandler.removeCallbacks(mUpdateTimeTask);
			mHandler.postDelayed(mUpdateTimeTask, 100);

			if (this.theme.viewStart != -1)
				((View) findViewById(this.theme.viewStart)).setVisibility(View.GONE);
			if (this.theme.viewResume != -1)
				((View) findViewById(this.theme.viewResume)).setVisibility(View.GONE);
			if (this.theme.viewPause != -1)
				((View) findViewById(this.theme.viewPause)).setVisibility(View.VISIBLE);
			if (this.theme.viewStop != -1)
				((View) findViewById(this.theme.viewStop)).setVisibility(View.VISIBLE);
			break;
		case Track.STATUS_STOPED:
			mHandler.removeCallbacks(mUpdateTimeTask);
			if (this.theme.viewStop != -1)
				((View) findViewById(this.theme.viewStop)).setVisibility(View.GONE);
			if (this.theme.viewStart != -1)
				((View) findViewById(this.theme.viewStart)).setVisibility(View.VISIBLE);
			break;
		case Track.STATUS_PAUSED:
			mHandler.removeCallbacks(mUpdateTimeTask);
			if (this.theme.viewPause != -1)
				((View) findViewById(this.theme.viewPause)).setVisibility(View.GONE);
			if (this.theme.viewResume != -1)
				((View) findViewById(this.theme.viewResume)).setVisibility(View.VISIBLE);
			break;
		}
	}

	public void onGpsChanged(int oldStatus, int newStatus)
	{
	}

	private Handler mHandler = new Handler();

	private Runnable mUpdateTimeTask = new Runnable()
	{
		public void run()
		{
			if (theme.textChronoTotal != -1)
				((TextView) findViewById(theme.textChronoTotal)).setText(((CycleMeterApplication) getApplication()).getTrack().getChronoTotalFormat());

			mHandler.postDelayed(this, 100);
		}
	};

	public void ShowHistory()
	{
		startActivity(new Intent(CycleMeterActivity.this, TrackHistoryActivity.class));
	}
}